﻿using B.R.RabbitRoot.Domain;
using B.R.RabbitRoot.Domain.RBAS;
using B.R.RabbitRoot.ErrorCode;
using B.R.RabbitRoot.Infrastructure.Interface;
using B.R.RabbitRoot.Read.API.Applications.Commands.LoginCommands;
using B.R.RabbitRoot.Read.API.Dtos;
using MediatR;

namespace B.R.RabbitRoot.Read.API.Applications.CommandHandlers.LoginCommandHandlers
{
    /// <summary>
    /// 登陆命令处理
    /// </summary>
    public class LoginQueryCommandHandler : IRequestHandler<LoginQueryCommand, APIResult<AccountDTO>>
    {
        private readonly IBaserepository<Account> accountRepsitoty;
        private readonly IBaserepository<Role> RoleRepsitoty;
        private readonly IBaserepository<User_Role> User_RoleRepsitoty;

        public LoginQueryCommandHandler(IBaserepository<Account> accountRepsitoty, IBaserepository<Role> roleRepsitoty, IBaserepository<User_Role> user_RoleRepsitoty)
        {
            this.accountRepsitoty = accountRepsitoty;
            RoleRepsitoty = roleRepsitoty;
            User_RoleRepsitoty = user_RoleRepsitoty;
        }

        /// <summary>
        /// 登录命令.FirstOrDefault(m=>m.AccountName==request.AccountName);
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<APIResult<AccountDTO>> Handle(LoginQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<AccountDTO> result=new APIResult<AccountDTO>();
            result.Code=ResultCode.登陆成功;
            result.Message=ResultCode.登陆成功.ToString();
            var entity =from a in await accountRepsitoty.GatAsync() 
                        join b in await User_RoleRepsitoty.GatAsync() on a.AccountId equals b.AccountId
                        join c in await RoleRepsitoty.GatAsync() on b.RoleId equals c.RoleId
                        where a.AccountName==request.AccountName
                        select new 
                        {
                            AccountName=a.AccountName,
                            AccountPass=a.AccountPass,
                            AccountNickName=a.AccountNickName,
                            RoleName=c.RoleName
                        };
           var list = entity.FirstOrDefault(m => m.AccountName == request.AccountName);
            try 
            {
                if (list == null)
                { 
                     throw new Exception("账号不存在");
                }
                if (list.AccountPass != request.AccountPass)
                {
                    throw new Exception("密码不正确");
                }
            }
            catch (Exception ex)
            {
                result.Code = ResultCode.登录失败;
                result.Message = ResultCode.登录失败.ToString()+ex.Message;
                return await Task.FromResult(result);
            }
            AccountDTO account = new AccountDTO();
            account.AccountName = list.AccountName;
            account.AccountNickName = list.AccountNickName;
            account.RoleName = list.RoleName;
            result.Data = account;
            return await Task.FromResult(result);
        }
    }
}
